<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <!-- <label class="el-form-item-label">ID</label>
        <el-input v-model="query.id" clearable placeholder="ID" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <label class="el-form-item-label">产品编号</label>
        <el-input v-model="query.productSerial" clearable placeholder="产品编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">销售商编号</label>
        <el-input v-model="query.vendorSerial" clearable placeholder="销售商编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <!-- <date-range-picker
          v-model="query.time"
          class="date-item"
        />
        <date-range-picker
          v-model="query.count"
          start-placeholder="countStart"
          end-placeholder="countStart"
          class="date-item"
        />
        <date-range-picker
          v-model="query.amount"
          start-placeholder="amountStart"
          end-placeholder="amountStart"
          class="date-item"
        /> -->
        <label class="el-form-item-label">创建时间</label>
        <date-range-picker
          v-model="query.createTime"
          class="date-item"
        />
        <!-- <date-range-picker
          v-model="query.updateTime"
          start-placeholder="updateTimeStart"
          end-placeholder="updateTimeStart"
          class="date-item"
        /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!-- <el-form-item label="销售商编号" prop="vendorSerial">
            <el-input v-model="form.vendorSerial" style="width: 370px;" />
          </el-form-item> -->

          <!-- <el-form-item label="产品编号" prop="productSerial">
            <el-input v-model="form.productSerial" style="width: 370px;" />
          </el-form-item> -->

          <el-form-item label="产品" prop="productSerial">
            <el-select
              v-model="form.productSerial"
              filterable
              style="width: 370px"
              placeholder="请选择"
            >
              <el-option
                v-for="item in productList"
                :key="item.serial"
                :label="item.name"
                :value="item.serial"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="销售商" prop="vendorSerial">
            <el-select
              v-model="form.vendorSerial"
              style="width: 370px"
              placeholder="请选择"
            >
              <el-option
                v-for="item in vendorList"
                :key="item.serial"
                :label="item.name"
                :value="item.serial"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数量" prop="count">
            <el-input v-model="form.count" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="销售时间" prop="time">
            <el-date-picker v-model="form.time" type="datetime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <!-- <el-table-column prop="id" label="ID" /> -->
        <el-table-column prop="productInfo.name" label="产品名称" />
        <el-table-column prop="productSerial" label="产品编号" />
        <el-table-column prop="vendorInfo.name" label="销售商名称" />
        <el-table-column prop="vendorSerial" label="销售商编号" />
        <el-table-column prop="amount" label="金额" />
        <el-table-column prop="count" label="数量" />
        <el-table-column prop="time" label="销售时间" />
        <el-table-column prop="createTime" label="创建时间" />
        <!-- <el-table-column prop="updateTime" label="更新时间" /> -->
        <el-table-column v-if="checkPer(['admin','productSale:edit','productSale:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudProductSale from '@/api/productSale'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { time: null, count: null, amount: null, id: null, vendorSerial: null, productSerial: null, createTime: null, updateTime: null }
export default {
  name: 'ProductSale',
  components: { pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '产品销售', url: 'api/productSale', idField: 'id', sort: 'id,desc', crudMethod: { ...crudProductSale }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'productSale:add'],
        edit: ['admin', 'productSale:edit'],
        del: ['admin', 'productSale:del']
      },
      rules: {
        time: [
          { required: true, message: '销售时间不能为空', trigger: 'blur' }
        ],
        count: [
          { required: true, message: '数量不能为空', trigger: 'blur' }
        ],
        amount: [
          { required: true, message: '金额不能为空', trigger: 'blur' }
        ],
        vendorSerial: [
          { required: true, message: '销售商编号不能为空', trigger: 'blur' }
        ],
        productSerial: [
          { required: true, message: '产品编号不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'vendorSerial', display_name: '销售商编号' },
        { key: 'productSerial', display_name: '产品编号' }
      ],
      vendorList: [],
      productList: []
    }
  },
  mounted() {
    this.getProductAction()
    this.getVenderAction()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getProductAction() {
      crudProductSale.getProduct().then((res) => {
        this.productList = res.content
        this.productList.forEach(element => {
          element.name = element.serial + '（' + element.name + '）'
        })
      }).catch(() => {
      })
    },
    getVenderAction() {
      crudProductSale.getVendor().then((res) => {
        this.vendorList = res.content
        this.vendorList.forEach(element => {
          element.name = element.serial + '（' + element.name + '）'
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped>

</style>
